<?php

class Sistema_PaginassiteController extends App_Controller_Action
{
    private $_table     = 'PAGINASSITE';
    private $_pk        = 'PAG_ID';
    private $_order_col = 'CTS_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'CAT_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'PAG_FILE'; 
    public  $dir = 'public/upload/paginassite/';
    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Paginassite();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Paginassite;
    }

    public function indexAction() {
        //$obj = new Sistema_Model_DbTable_Paginassite();
        //$result = $obj->fetchAll();
        //$this->view->result = $this->paginacao($result);
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('dominio','library');
        
        $where = 'p.'.$this->_pk . ' > 0';
       
        $filtroseg = $this->_getParam('filtroseg', ''); 
        $dscfiltro = $this->_getParam('dscfiltro', '');
        
        if($filtroseg > '') $where .= ' and p.CTS_ID = '. $filtroseg;
        
        if($dscfiltro > '') $where .= ' and p.PAG_TITULO like "%'. $dscfiltro.'%"';
        
        $this->view->filtroseg = $filtroseg;
        $this->view->dscfiltro = $dscfiltro; 
        
        //$result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $query = 'SELECT SQL_CACHE * FROM PAGINASSITE p, CATSEGMENTO s WHERE p.CTS_ID = s.CTS_ID AND '.$where.' ORDER BY p.'.$this->_order_col.' '.$this->_order;
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
        $this->view->db = $this->db;
        
    }
    
    
    public function editAction() {
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/ckeditor/ckeditor.js');
        
        $form = new Sistema_Form_Paginassite();
        
        $option = array('width'=>'70%');
        echo $this->view->CkeditorHelper('PAG_RESUMO',$option);
        echo $this->view->CkeditorHelper('PAG_TEXTO',$option);
        echo $this->view->CkeditorHelper('PAG_TEXTO2',$option);
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int) $form->getValue('PAG_ID');
                
                $returnImg = imagem::upload('PAG_FILE',$this->dir,$id);
                
                $data =  $form->getValues();
                
                unset($data['img']);
                unset($data['PAG_FILE']);
                
                $obj = new Sistema_Model_DbTable_Paginassite();
                
                if ($id == 0){
                    $obj->insert($data);
                    $id =  $obj->getAdapter()->lastInsertId();                    
                    imagem::rename($id,$this->dir,$returnImg['filenew']);
                }
                else
                    $obj->update($data, 'PAG_ID = ' . $id);
                
                /*
                if(strlen($_FILES['PAG_FILE']['tmp_name']) > 1){
                    $img = glob($this->dir.$id.'.*');
                    foreach($img as $filetemp) $filename = $filetemp;
                    imagem::reizeFull($filename,$filename, 900, 200);
                }
                */
                
                $this->_helper->redirector('index'); 
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $obj = new Sistema_Model_DbTable_Paginassite();
                $form->populate($obj->fetchRow('PAG_ID = ' . $id)->toArray());
            }
        }
    }

    public function deleteAction() {
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        $ok = FALSE;
        
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {
            $obj = new Sistema_Model_DbTable_Paginassite();
            $ok = (bool) $obj->delete('PAG_ID = '.(int)$id);
            
            Zend_Loader::loadClass('imagem','library');
            imagem::delete($id,$this->dir);
            //$this->_helper->redirector('index'); 
        } else {
            //$id = $this->_getParam('id', 0);
            //$obj = new Sistema_Model_DbTable_Paginassite();
            //$form->populate($obj->fetchRow('PAG_ID = ' . $id)->toArray());
        }
        
        echo json_encode(array('ok' => $ok));
    }
}